﻿#ifndef NETMANGER_TCP_H
#define NETMANGER_TCP_H


#define USERMAX 50   //映射的最大用户数量
#include "QTcpSocket"
#include "QTimer"


class netManger_tcp
{
public:

    struct socketMsg{
        QString serverAddress = ""; //映射服务器连接的客户的地址信息
        uint16_t localPort = 0; //本地的服务端口
        QTcpSocket* socket = nullptr; //Tcp连接的套接字
        uint32_t signNumber = 0; //存货标记，0为不判断
    };


    std::function<void(QTcpSocket*)> event_localReadyRead = nullptr;
    std::function<void(QTcpSocket*)> event_disConnected = nullptr;

private:
    QVector<socketMsg> userList; //本地客户端的连接数量
    QTimer* lifeTimer = nullptr; //本地TCP客户端回收周期，长时间未通讯则进行自动回收
    uint32_t signNumber = 0; //用于每次检查标记，检查是否超越这个标记，超越标记的不回收
    uint32_t lifeNumber = 0; //每一次客户端被信息调用步进一次

public:
    netManger_tcp(std::function<void(QTcpSocket*)> localReadyReadFun, std::function<void(QTcpSocket*)> disConnectFun); //构造函数，并且传入本地服务器给本地客户端的对象
    ~netManger_tcp();

    QTcpSocket* addUser(QString serverAddress,uint16_t localPort); //添加服务器用户到用户列表
    bool removeUser(QString serverAddress); //移除用户
    bool removeUser(QTcpSocket* socket); //移除用户
    uint32_t getUserNumber(); //获取用户数量
    QString getServerAddress(QTcpSocket* socket); //根据Socket对象，返回服务器标记
    QTcpSocket* getSocket(QString address); //工具标记，获取Socket对象
    void clear(); //清理全部的接口


    bool event_connect(QString serverAddress,uint16_t localPort); //客户连接
    bool event_readyRead(QString serverAddress,QByteArray data); //收到数据
    bool event_disConnect(QString serverAddress); //客户离开


    //bool event_localReadyRead(QTcpSocket* localUser);//映射的本地端服务器返回数据给客户,本地服务器决策

};

#endif // NETMANGER_TCP_H
